package org.example.flink

import org.apache.flink.api.scala._

object Hello02WordCountByDataSetUseScala {
  def main(args: Array[String]): Unit = {
    //创建环境
    val environment: ExecutionEnvironment = ExecutionEnvironment.getExecutionEnvironment
    //读取数据源
    val source: DataSet[String] = environment.readTextFile("data/wordcount.txt")
    //transform
    val wordSet: DataSet[String] = source.flatMap(_.split(" "))
    val mapSet: DataSet[(String, Int)] = wordSet.map((_, 1))
    //开始计数
    val groupBySet: GroupedDataSet[(String, Int)] = mapSet.groupBy(0)
    //开始分类并统计
    val sumSet: AggregateDataSet[(String, Int)] = groupBySet.sum(1)
    //打印结果
    sumSet.print()
  }
}